import 'dart:io';

import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
import 'package:flutter_ffmpeg/media_information.dart';
import 'package:flutter_ffmpeg/stream_information.dart';
import 'package:flutter_ffmpeg_demo/util/pathUtil.dart';
import 'package:flutter_ffmpeg_demo/util/util.dart';

class FfmpegUtil {
  static final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();
  static final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();
  static Future<MediaInformation> getMediaInformation(String path) async {
    return await _flutterFFprobe.getMediaInformation(path);
  }

  static getInfo(String path) {
    print(path);
    getMediaInformation(path)
        // getMediaInformation('/data/user/0/com.example.test_flutter_ffmpeg/cache/video_wrap.png')
        .then((information) {
      print('------get info---');
      print(information);
      if (information.getMediaProperties() != null) {
        ffprint("---");
        if (information.getMediaProperties().containsKey('filename')) {
          ffprint('Path: ${information.getMediaProperties()['filename']}');
        }
        if (information.getMediaProperties().containsKey('format_name')) {
          ffprint("Format: " + information.getMediaProperties()['format_name']);
        }
        if (information.getMediaProperties().containsKey('bit_rate')) {
          ffprint("Bitrate: " + information.getMediaProperties()['bit_rate']);
        }
        if (information.getMediaProperties().containsKey('duration')) {
          ffprint("Duration: " + information.getMediaProperties()['duration']);
        }
        if (information.getMediaProperties().containsKey('start_time')) {
          ffprint(
              "Start time: " + information.getMediaProperties()['start_time']);
        }
        if (information.getMediaProperties().containsKey('nb_streams')) {
          ffprint("Number of streams: " +
              information.getMediaProperties()['nb_streams'].toString());
        }
        Map<dynamic, dynamic> tags = information.getMediaProperties()['tags'];
        if (tags != null) {
          tags.forEach((key, value) {
            ffprint("Tag: " + key + ":" + value);
          });
        }

        List<StreamInformation> streams = information.getStreams();
        if (streams != null) {
          for (var i = 0; i < streams.length; ++i) {
            StreamInformation stream = streams[i];
            ffprint("---");
            if (stream.getAllProperties().containsKey('index')) {
              ffprint("Stream index: " +
                  stream.getAllProperties()['index'].toString());
            }
            if (stream.getAllProperties().containsKey('codec_type')) {
              ffprint(
                  "Stream type: " + stream.getAllProperties()['codec_type']);
            }
            if (stream.getAllProperties().containsKey('codec_name')) {
              ffprint(
                  "Stream codec: " + stream.getAllProperties()['codec_name']);
            }
            if (stream.getAllProperties().containsKey('codec_long_name')) {
              ffprint("Stream full codec: " +
                  stream.getAllProperties()['codec_long_name']);
            }
            if (stream.getAllProperties().containsKey('pix_fmt')) {
              ffprint("Stream format: " + stream.getAllProperties()['pix_fmt']);
            }
            if (stream.getAllProperties().containsKey('width')) {
              ffprint("Stream width: " +
                  stream.getAllProperties()['width'].toString());
            }
            if (stream.getAllProperties().containsKey('height')) {
              ffprint("Stream height: " +
                  stream.getAllProperties()['height'].toString());
            }
            if (stream.getAllProperties().containsKey('bit_rate')) {
              ffprint(
                  "Stream bitrate: " + stream.getAllProperties()['bit_rate']);
            }
            if (stream.getAllProperties().containsKey('sample_rate')) {
              ffprint("Stream sample rate: " +
                  stream.getAllProperties()['sample_rate']);
            }
            if (stream.getAllProperties().containsKey('sample_fmt')) {
              ffprint("Stream sample format: " +
                  stream.getAllProperties()['sample_fmt']);
            }
            if (stream.getAllProperties().containsKey('channel_layout')) {
              ffprint("Stream channel layout: " +
                  stream.getAllProperties()['channel_layout']);
            }
            if (stream.getAllProperties().containsKey('sample_aspect_ratio')) {
              ffprint("Stream sample aspect ratio: " +
                  stream.getAllProperties()['sample_aspect_ratio']);
            }
            if (stream.getAllProperties().containsKey('display_aspect_ratio')) {
              ffprint("Stream display aspect ratio: " +
                  stream.getAllProperties()['display_aspect_ratio']);
            }
            if (stream.getAllProperties().containsKey('avg_frame_rate')) {
              ffprint("Stream average frame rate: " +
                  stream.getAllProperties()['avg_frame_rate']);
            }
            if (stream.getAllProperties().containsKey('r_frame_rate')) {
              ffprint("Stream real frame rate: " +
                  stream.getAllProperties()['r_frame_rate']);
            }
            if (stream.getAllProperties().containsKey('time_base')) {
              ffprint("Stream time base: " +
                  stream.getAllProperties()['time_base']);
            }
            if (stream.getAllProperties().containsKey('codec_time_base')) {
              ffprint("Stream codec time base: " +
                  stream.getAllProperties()['codec_time_base']);
            }

            Map<dynamic, dynamic> tags = stream.getAllProperties()['tags'];
            if (tags != null) {
              tags.forEach((key, value) {
                ffprint("Stream tag: " + key + ":" + value);
              });
            }
          }
        }
      }
    });
  }

  static overlayVideo(String path) async {
    var videoFile = new File(path + '.overlay.mp4');
    String assetName = 'video_wrap.png';
    // final String fullTemporaryPath = await PathUtil.assetPath('video_wrap.png');
    File file = await PathUtil.assetToFile(assetName);
    print('-----file------');
    print(file.path);
    try {
      videoFile.delete().catchError((_) {});
    } on Exception catch (_) {}
    // String cmdStr =
    //     '-f image2 -i $fullTemporaryPath -r 0.25 ${path}/test.mp4 -y';
    // String cmdStr =
    //     '-i $path -i ${file.path} -filter_complex "[1][0]scale2ref=iw:ih[wm][vid];[vid][wm]overlay=17:17[out]" ${videoFile.path}';
    String cmdStr =
        '-i $path -vf "movie=${file.path} [watermark]; [in][watermark] overlay=0:0 [out]" ${videoFile.path}';

    var rc = await _flutterFFmpeg.execute(cmdStr);
    print("FFmpeg process exited with rc $rc");
    print('---------转换完毕-----------');
    return videoFile;
  }
}
